﻿using System;
using System.Collections.Generic;

using GameTools;

namespace PuSuoPai
{
	public class NBGame 
	{
		static public readonly NBGame Instance = new NBGame();

		public enum MODE_TYPE
		{
			娱乐,
			预测,
			
			MAX
		}

		public enum DIFFCULT_TYPE
		{
			非常简单,
			简单,
			普通,
			困难,
			非常困难,
			地狱,

			MAX
		}

		public MODE_TYPE Mode = MODE_TYPE.娱乐;
		public DIFFCULT_TYPE Diffcult = DIFFCULT_TYPE.普通;
		public List<byte>[] BlockCards = new List<byte>[BLOCK_CARD_NUM];
		public byte TopCard = 0;
		public List<byte> RemainCards = new List<byte>();
		public String Title { get; set; }
		public int[] CurCardResult = new int[2];
		public bool IsPlaying { get; set; }

		public const int CARD_COUNT = 198;
		public const int BLOCK_CARD_NUM = 9;
		public const int REFRESH_TOP_CARD_NUM = 14;
		public const float REFRESH_FAST_RATE = 0.8f;		// 前50张速度慢些
		public const float REFRESH_SLOW_RATE = 1.2f;		// 后17张速度快些

		float[] mGameTime = new float[(int)DIFFCULT_TYPE.MAX]{350, 300, 250, 200, 150, 100};

		float mRefreshTime = 0;
		DateTime mStartTime = DateTime.Now;
		DateTime mLastRefreshTime = DateTime.Now;
		int mCurRefreshTopCardNum = 0;

		public void Init()
		{
			mRefreshTime = mGameTime[(int)Diffcult] / (CARD_COUNT - 81);
			IsPlaying = false;

			TopCard = 0;
			mCurRefreshTopCardNum = 0;
			
			CurCardResult[0] = 0;
			CurCardResult[1] = 0;
			RemainCards.Clear();
		}

		public void Start()
		{
			if (IsPlaying)
				return;

			IsPlaying = true;
			mStartTime = DateTime.Now;
			mLastRefreshTime = DateTime.Now;

			// init remain cards
			RemainCards.Clear();
			for (int i=1; i<= CARD_COUNT; ++i)
			{
				RemainCards.Add((byte)i);
			}

			// init block cards
			for (int i=0; i<BlockCards.Length; ++i)
			{
				if (BlockCards[i] == null)
					BlockCards[i] = new List<byte>();
				else
					BlockCards[i].Clear();

				for (int j=0; j<BLOCK_CARD_NUM; ++j)
				{
					byte cardId = GetNextCard();
					BlockCards[i].Add(cardId);
				}
			}

			TopCard = GetNextCard();
			mCurRefreshTopCardNum = 0;

			CurCardResult[0] = 0;
			CurCardResult[1] = 0;
		}

		public void Update()
		{
			if (!IsPlaying)
				return;

			if (Mode == MODE_TYPE.娱乐)
			{
				if (RemainCards.Count == 0
				    || (DateTime.Now - mStartTime).TotalSeconds >= mGameTime[(int)Diffcult])
				{
					Over();
					return;
				}

				double interval = (DateTime.Now - mLastRefreshTime).TotalSeconds;

				float refreshTime = mRefreshTime;
				if (RemainCards.Count > 67)
					refreshTime *= REFRESH_SLOW_RATE;
				else if (RemainCards.Count <= 17)
					refreshTime *= REFRESH_FAST_RATE;

				if (interval >= mRefreshTime)
				{
					mLastRefreshTime = DateTime.Now;

					RefreshTopCard();
				}
			}
		}

		public void Over()
		{
			if (!IsPlaying)
				return;

			IsPlaying = false;

			int remainBlockCards = 0;
			int point = 0;
			int[] RemainBlockCount = new int[2];
			foreach (var info in BlockCards)
			{
				remainBlockCards += info.Count;

				if (info.Count > 0)
					point += info[0];

				if (info.Count > RemainBlockCount[0])
				{
					RemainBlockCount[1] = RemainBlockCount[0];
					RemainBlockCount[0] = info.Count;
				}
				else if (info.Count > RemainBlockCount[1])
				{
					RemainBlockCount[1] = info.Count;
				}
			}

			// 计算得分
			if (Mode == MODE_TYPE.娱乐)
			{
				// 获取称号
				if (remainBlockCards < 10)	Title = "最聪明的人";
				else if (remainBlockCards < 30) Title = "聪明人";
				else if (remainBlockCards < 40) Title = "需要进步的人";
				else Title = "每天必须训练的人";

				if (81 - remainBlockCards < 12)
				{
					CurCardResult[0] = 0;
				}
				else
				{
					int ret = point / 6;
					if (ret < 1 || ret > 198)
						ret = ret % 6 + 1;

					CurCardResult[0] = ret;
				}
			}
			else if (Mode == MODE_TYPE.预测)
			{
				int[] rets = new int[2];
				foreach (var info in BlockCards)
				{
					if (info.Count == RemainBlockCount[0])
						rets[0] += info[0];
					else if (info.Count == RemainBlockCount[1])
						rets[1] += info[0];
				}

				for (int i=0; i<rets.Length; ++i)
				{
					rets[i] = rets[i] / 6;
					if (rets[i] < 1 || rets[i] > 198)
						rets[i] = rets[i] % 6 + 1;
					
					CurCardResult[i] = rets[i];
				}
			}
		}

		public byte GetNextCard()
		{
			if (RemainCards.Count == 0)
				return 0;

			int index = GameHelper.RandomSource.Next(RemainCards.Count);
			byte cardId = RemainCards[index];
			RemainCards.RemoveAt(index);

			return cardId;
		}

		public void RefreshTopCard()
		{
			if (Mode == MODE_TYPE.预测)
			{
				if (++mCurRefreshTopCardNum > REFRESH_TOP_CARD_NUM)
				{
					Over();
					return;
				}
			}

			TopCard = GetNextCard();
		}

		public void SelectBlock(int index)
		{
			if (!IsPlaying)
				return;

			byte curCardId = 0;
			if (BlockCards[index].Count > 0)
			{
				curCardId = BlockCards[index][0];
			}

			if (curCardId != 0)
			{
				// 如果个位数相同，则正确
				if (TopCard%10 == curCardId%10)
				{
					BlockCards[index].RemoveAt(0);
				}
				else
				{
				}
			}
		}
	}
}
